#ifndef SIPB2BSESSION_HPP__
#define SIPB2BSESSION_HPP__

#include "WTSXSipService/SipWTSXServiceItf.hpp"
#include "WTSXSipService/SipSessionBase.hpp"

#include <boost/thread/thread.hpp>

using namespace resip;

namespace wtsxsip{


class WTSX_SIPSRV_PUBLIC CSipB2BSession: 
    public CSipSessionBase, 
    public ISipTranSessionHandle,
    public ISipDlgSessionBaseHandle
{
public:

    CSipB2BSession(CSipSessionMgr& mgr, uint32_t id, uint32_t calleeId, uint32_t callerId);

    virtual ~CSipB2BSession();

    virtual bool start();

    virtual bool start(const SMsgBodyBase& body);

    virtual bool destroy();

protected:

    void CSipB2BSession::destroyThread();

    bool insertToTranMap(uint32_t callerTranId, uint32_t calleeTranId);

    bool earaseFromTranMap(uint32_t callerTranId);

    uint32_t getCalleeIdFromTranMap(uint32_t callerTranId);

    /**  SipSessionBase methods  */

    virtual bool messageDress(wtsip::CSipMessage& msg);

    virtual bool onDestroy();

    /**  ISipTranSessionHandle methods */

    virtual bool onRequest(ISipTranSession& tran, const SHeaders& headers, const SMsgBodyBase& body);

    virtual bool onResponse(ISipTranSession& tran, const SHeaders& headers, const SMsgBodyBase& body);

    virtual bool onState(ISipTranSession& tran, const ISipTranSession::ETranSessionState newState);

    virtual bool onDestroy(ISipTranSession& tran);

    /**  ISipDlgSessionBaseHandle methods */

    virtual bool onRequest(ISipDlgSessionBase& dlg, const SHeaders& headers, 
        const SMsgBodyBase& body, ISipTranSession& srvTran);

    virtual bool onResponse(ISipDlgSessionBase& dlg, const SHeaders& headers,
        const SMsgBodyBase& body, ISipTranSession& clnTran);

    virtual bool onState(ISipDlgSessionBase& dlg, const ISipDlgSessionBase::EDlgSessionState newState);

    virtual bool onDestroy(ISipDlgSessionBase& dlg);

protected:

    //int m_destroyCounter;

    uint32_t m_calleeId;

    uint32_t m_callerId;

    bool m_started;

    //bool m_destroying;

    boost::thread m_desThread;

    boost::shared_mutex m_MapMutex;

    /** Caller tran id  ---  Callee tran id*/
    std::map<uint32_t, uint32_t> m_TransMap;
};



}//namespace

#endif


